import { messages } from "share/common";
import React, { Component } from "react";
import { connect } from "react-redux";
import { Modal, Button } from "antd";
import { openModal, removeContainer } from "components/method/openModal";
import CertificatePreview from "../../new-finance-audit/certificate-preview";

@connect(mapStateToProps)
class Credential extends Component {
  render() {
    const { visible, afterClose, onCancel, templateCodeList, record, loginUser } = this.props;
    return (
      <Modal
        visible={visible}
        footer={<Button onClick={onCancel}>{messages("finance-10.key79") /*关闭*/}</Button>}
        onCancel={onCancel}
        title={messages("finance-10.key80") /*凭证预览*/}
        afterClose={afterClose}
      >
        <CertificatePreview
          templateCodeList={templateCodeList}
          bookDate={record.bookDate}
          bookDateType={1000}
          params={{
            businessCode: record.batchNumber,
            entityType: 1301,
            setOfBooksId: record.setOfBooksId,
            operatorOid: loginUser.userOID,
            forceRefresh: false,
          }}
        />
      </Modal>
    );
  }
}

Credential.open = function (props, env) {
  let container;
  const targetProps = {
    ...props,
    visible: true,
    onCancel: () => close(),
    afterClose: () => {
      removeContainer(container);
      container = null;
    },
  };
  function close() {
    openModal(
      Credential,
      {
        ...targetProps,
        visible: false,
      },
      env,
      container
    );
  }
  container = openModal(Credential, targetProps, env);
};

function mapStateToProps(state) {
  return {
    loginUser: state.login.loginUser,
  };
}

export default Credential;
